package com.ndhutu.ee.util.easemob.im;

import com.alibaba.fastjson.JSON;
import com.ndhutu.ee.util.http.OKHttpUtil;
import okhttp3.*;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 环信IM即时通讯工具类
 * @Author Shane
 * @Date 2019/8/3011:28
 * @param
 * @return
 **/
public class IMUtil {
    public static String getUrl(IMConfig config){
        return config.getUrl()+"/"+config.getOrgName()+"/"+config.getAppName()+"/";
    }

    /**
     * 获取管理员权限
     * @Author Shane
     * @Date 2019/8/3015:03
     * @param
     * @return
     **/
    public static String getToken(IMConfig config) throws Exception{
        String url = getUrl(config)+"token";

        Map<String, String> params = new HashMap<String, String>();
        params.put("grant_type", "client_credentials");
        params.put("client_id", config.getClientId());
        params.put("client_secret", config.getClientSecret());

        return OKHttpUtil.post(url, JSON.toJSONString(params));
    }

    /**
     * 注册用户
     * @Author Shane
     * @Date 2019/8/3014:38
     * @param token     管理员身份凭证
     * @param userId    用户id（唯一）
     * @param password  登录密码（建议全部一样）
     * @param nickname  昵称
     * @return
     **/
    public static String createUser(IMConfig config, String token, Integer userId, String password, String nickname) throws Exception{
        String url = getUrl(config)+"users";

        Map<String, String> params = new HashMap<String, String>();
        params.put("username", userId+"");
        params.put("password", password);
        params.put("nickname", nickname);

        RequestBody body = RequestBody.create( MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(params));
        Request request = new Request.Builder()
                .url(url)
                .addHeader("Authorization", "Bearer "+token)
                .post(body)
                .build();
        Call call = new OkHttpClient().newCall(request);
        Response response = call.execute();
        return response.body().string();
    }

    public static void upload(IMConfig config, File file) throws Exception{

    }


    public static void sendMsg(List<String> usernames, String msg, String type) throws Exception{

    }



    public static void main(String[] args) throws Exception {
        String url = "http://a1.easemob.com";
        String orgName = "1116190830148221";
        String appName = "ppfriend";
        String clientId = "YXA62HjdLoTsRSan530pP9u-xw";
        String clientSecret = "YXA6MJ0CqHtEsE8hSO-n5qjwfuwQonE";
        IMConfig config = new IMConfig(url, orgName, appName, clientId, clientSecret);

//        String  token = getToken(config);
//        System.out.println(token);


        String token = "YWMtGHMGSsvMEem2WxUJTofLiQAAAAAAAAAAAAAAAAAAAAHYeN0uhOxFJqfnfSk_277HAgMAAAFs5tx9ygBPGgBYk5YtIXTf3l-rh2zqdOnFf7xrVjL6AvEzbvIMkHhCWA";

        String result = createUser(config, token, 1, "123456", "超级管理员");
        System.out.println(result);
    }
}
